package com.ziq.zcamp.community

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.gmlive.camp.data.exception.LocalException
import com.ziq.base.extension.launchWithTryAsynchronous
import com.ziq.zcamp.R
import com.ziq.zcamp.api.repository.ActivitiesRepository
import com.ziq.zcamp.api.repository.DefaultActivitiesRepository
import com.ziq.zcamp.api.service.NetWorkException
import com.ziq.zcamp.api.service.ServiceModule.Companion.onApiFail
import com.ziq.zcamp.bean.activities.ActivitiesInfo
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.view.StatusView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * 首页 ViewModel
 */
class CommunityActivitiesViewModel : ViewModel() {

    private val mRepository : ActivitiesRepository by lazy {
        DefaultActivitiesRepository()
    }
    private var currentPage = 1
    private var hasMore = true
    private val size = 20

    val hasMoreData = MutableLiveData(true)
    val statusShow = MutableLiveData(StatusView.NO_DATA)
    val recommendShow = MutableLiveData<Boolean>()
    val commonActivitiesList = MutableLiveData<MutableList<ActivitiesInfo>>()
    val recommendedActivitiesList = MutableLiveData<MutableList<ActivitiesInfo>>()

    fun requestCommonList(isFirst : Boolean) {
        launchWithTryAsynchronous(whenTry = {
            if (isFirst){
                currentPage = 1
                hasMore = true
                if (commonActivitiesList.value?.isEmpty() == true){
                    statusShow.postValue(StatusView.LOADING)
                }
            }
            if (hasMore){
                val result = mRepository.obtainActivityList(currentPage,size)
                currentPage ++
                hasMore = result.has_more?:false
                hasMoreData.postValue(hasMore)
                val tempList = mutableListOf<ActivitiesInfo>()
                if (!isFirst){
                    tempList.addAll(commonActivitiesList.value?: mutableListOf())
                }
                tempList.addAll(result.list?: mutableListOf())
                withContext(Dispatchers.Main){
                    commonActivitiesList.value = tempList
                    if (commonActivitiesList.value.isNullOrEmpty()){
                        statusShow.postValue(StatusView.NO_DATA)
                    }else{
                        statusShow.postValue(StatusView.IDLE)
                    }
                }
            }
        }, whenCatch = {
            if (commonActivitiesList.value.isNullOrEmpty()){
                statusShow.postValue(StatusView.NET_ERROR)
            }else{
                statusShow.postValue(StatusView.IDLE)
            }
            withContext(Dispatchers.Main){
                when(it){
                    is NetWorkException -> {
                        onApiFail(it.errorMsg)
                    }
                    is LocalException -> {
                        onApiFail(GlobalContextManager.getString(R.string.all_net_error))
                    }
                }
            }
        })
    }

    fun requestRecommendList() {
        launchWithTryAsynchronous(whenTry = {
            val result = mRepository.activityRecommendedList()
            withContext(Dispatchers.Main){
                recommendedActivitiesList.value = mutableListOf<ActivitiesInfo>().apply { addAll(result.list?: mutableListOf()) }
                recommendShow.postValue(recommendedActivitiesList.value?.isNotEmpty() == true && (recommendedActivitiesList.value?.size?:0) >= 2)
            }
        }, whenCatch = {
        })
    }


}